<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.lynn.salary.pojo.TreeNode"%>
<%@page import="com.opensymphony.xwork2.util.ValueStack"%>
<%@page import="com.lynn.salary.entity.User_"%>
<%@page import="com.lynn.salary.entity.SystemArgument"%>
<%@page import="com.lynn.salary.util.ToolMethod"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
	+ request.getServerName() + ":" + request.getServerPort()
	+ path + "/";
%>

<link rel="stylesheet" href="<%=basePath%>style/jquery.treeview.css" />
<script src="<%=basePath%>js/jquery.treeview.js" type="text/javascript"></script>
<%
	ValueStack vs = (ValueStack) request.getAttribute("struts.valueStack");
	List<TreeNode> all = (List<TreeNode>)vs.findValue("all");
	
	String randomClass = ToolMethod.getCharAndNumr(6);
	String treeId = ToolMethod.getCharAndNumr(6);
%>
<div>
<h1>组织结构</h1>
<br/>
<table>
<tr>
	<td>
	<input id="searchInput-<%=treeId%>" style="width:120px;display: inline;">
	</td>
	<td>
	<img id="search-<%=treeId%>" alt="" src="<%=basePath%>images/008.jpg" style="display: inline; padding-left: 5px;cursor: pointer;">
	</td>
</tr>
</table>

<div id="<%=treeId%>-control" style="padding-top: 10px;padding-bottom: 10px;">
	<span>操作:</span>
	<a title="关闭所有" href="#"><img src="<%=basePath%>images/minus.gif" />关闭</a>
	<a title="展开所有" href="#" id="expandAll-<%=treeId%>"><img src="<%=basePath%>images/plus.gif" />展开</a>
</div>
<ul id="<%=treeId%>" class="filetree">
	<%
		boolean first = true;
		for(TreeNode node : all) {
	%>
	<%
		if(first) { 
		first = false;
	%>
		
		<li>
	<%
		} else {
	%>
		<li class="closed">
	<%
		}
	%>
	
			<span class="folder <%=randomClass%>" flag="<%=node.getFlag()%>" objId="<%=((SystemArgument)node.getData()).getCode()%>" name="<%=node.getName()%>">
				<%=node.getName()%>
			</span>
	
			<%
					if(node.getChilds()!=null) {
				%>
			<ul>
				<%
					for(TreeNode c : node.getChilds()) { 
									User_ e = (User_)c.getData();
				%>
					<li item="<%=randomClass %>" itemname="<%=c.getName() %>">
						<a class="<%=randomClass %>"  href="javascript:;" flag="<%=c.getFlag() %>" objId="<%=e.getId() %>" name="<%=e.getName() %>">
							<span class="file">
								<%=c.getName() %>
							</span>
						</a>
					</li>
				<%} %>
			</ul>
			<%} %>
		</li>
	<%
		}
	%>
</ul>

</div>
<script>
$("#<%=treeId %>").treeview({
	control: "#<%=treeId%>-control",
});
var lynn = {};
var treeName;
lynnAddLis = function (lis) {
	lynn[treeName].push(lis);
}
lynnInitTree = function () {
	treeName = "<%=treeId %>";
	lynn[treeName] = [];
	$(".<%=randomClass %>").click(function(event){
			var i = 0;
			for(i=0;i<lynn[treeName].length;i++) {
				var flag = $(event.currentTarget).attr("flag");
				var objId = $(event.currentTarget).attr("objId");
				var name = $(event.currentTarget).attr("name");
				lynn[treeName][i](flag,objId,name);
			}
		}
	);
}
var search = function (tn) {
$("li[item=<%=randomClass %>]").each(function(idx,elem){
	var nn = $(elem).attr("itemname");
	//展开所有元素
	$("a#expandAll-<%=treeId %>").trigger('click');
	if(nn.indexOf(tn) > -1){
		$(elem).css("display","block");
	} else {
		$(elem).css("display","none");
	}
});
}

$("img#search-<%=treeId %>").click(function(){
var v = $("input#searchInput-<%=treeId %>").attr("value");
search(v);
});
$("input#searchInput-<%=treeId %>").keyup(function(event){
	if(event.keyCode == 13) {
		var v = $("input#searchInput-<%=treeId %>").attr("value");
		search(v);
	}
} );
</script>

